var testdata;
var designPatternCategoryId;

function showDialog(dpcId){
	designPatternCategoryId = dpcId;
	$("#use-designPatternCategory-panel").modal({ backdrop:false, show:true});
}

function importDesignPatternComponents(projectId){
	$.ajax({
		type : "POST",
		url : 'designPatternCategory/import',
		data : {
			'projectId' : projectId,
			'designPatternCategoryId' : designPatternCategoryId,
		},
		success : function(data) {
			if(data.result == "true"){
				alert("导入成功!");
			}else{
				alert("导入失败!");
			}
		}});
}

function queryDesignPatternCategory(pageNum) {
	var name = $("#designPatternCategory_name").val();
	var description = $("#designPatternCategory_description").val();
	var groupid =  $("#groupid").val();
	$.cookie('query_name', name);
	$.cookie('query_des', description);
	$.cookie('groupid', groupid);
	$.ajax({
				type : "POST",
				contentType : "application/x-www-form-urlencoded; charset=utf-8",
				url : 'designPatternCategory/query',
				dataType : "json",
				data : {
					'pageNum' : pageNum,
					'query_name' : name,
					'query_des' : description,
					'groupid' : groupid
				},
				success : function(data) {
					testdata = data;
					if (data.success == "true") {
						var currentNum = data.currentNum;
						var totalNum = data.totalNum;
						$("#search_reuslt").hide();
						$("#desingPatternCategoryList").show();
						$("#desingPatternCategoryList tbody").html("");
						var rows = $("<tbody></tbody>");
						$.each(
								data.designPatternCategorys,
								function(n, value) {
									var tr = $("<tr></tr>");
									var selectTd = $("<td id='td_uri'></td>").append(n + 1);
									var nameTd = $("<td></td>").append(value.name);
									var ownerTd = $("<td></td>").append(value.owner.username);
									var desTd = $("<td></td>").append(value.description);
									var operationTd;
									operationTd = $("<td></td>").append("<div class='btn-group'><button class=\"btn\" onclick=\"window.location.href='/ManEnv/designPatternCategory/" 
											+ value.id + "/entityZone/scan';\"><i class=\"icon-search\"></i>&nbsp;查看</button><button class=\"btn\""+ 
                                            " onclick=\"showDialog(" + value.id + ")\"><i class=\"icon-edit\"></i>&nbsp;使用</button></div>");
											tr.append(selectTd).append(nameTd).append(ownerTd).append(desTd).append(operationTd).appendTo(rows);
									});
						
						$("#desingPatternCategoryList tbody").html(rows.html());
						$("#desingPatternCategoryList tfoot").html("");
						//分页
						/*var footTr = $("<tr></tr>");
						var footTd = $("<td></td>");
						footTd.attr("colspan","4");
						var div =  $("<div class=\"pagination pagination-centered\"></div>");
						var ul = $("<ul></ul>");
						if(currentNum == 1){
							ul.append("<li class=\"disabled\"><a href=\"javascript:void(0);\">&laquo;</a></li>");
						}else{
							var temp = currentNum -1;
							ul.append("<li><a href=\"javascript:void(0);\" onclick='queryTemplate("+temp+")'>&laquo;</a></li>");
						}
						
						ul.append("<li class=\"active\"><a href=\"javascript:void(0);\">"+currentNum+"</a></li>");
						for(var i=1;i<5;i++){
							var temp = currentNum +i;
							if(temp > totalNum){
								ul.append("<li class=\"disabled\"><a href=\"javascript:void(0);\">"+temp+"</a></li>");
							}else{
								ul.append("<li><a href=\"javascript:void(0);\" onclick='queryTemplate("+temp+")'>"+temp+"</a></li>");
							}
						}
								
						if(currentNum +1 > totalNum){							
							ul.append("<li class=\"disabled\"><a href=\"javascript:void(0);\">&raquo;</a></li>");
						}else{
							var temp = currentNum +1;
							ul.append("<li><a href=\"javascript:void(0);\" onclick='queryTemplate("+temp+")'>&raquo;</a></li>");
						}
						div.append(ul).appendTo(footTd);
						footTd.appendTo(footTr);
						
						$("#desingPatternCategoryList tfoot").html(footTr.html());*/
					} else {
						$("#search_reuslt").show();
						$("#search_reuslt").text("系统内未检索到相关的设计模式组件");
						$("#desingPatternCategoryList").hide();
					}
				}
			});
}